package com.cy.pj.goods.dao;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 *
 *
 * @Repository//此注解通常用于描述数据层实现对象，本质上就是一个特殊的@Component，
 * 都是要交给spring框架管理的一个bean对象
 */

@Repository
public class DefaultGoodsDao implements GoodsDao{
    @Autowired
    private DataSource dataSource;

    public List<Map<String,Object>> findGoods(){
        //1，获取连接（从连接池获取）
        Connection conn=null;
        Statement stmt=null;
        ResultSet rs=null;
        String sql="select * from tb_goods";
        try {
            conn=dataSource.getConnection();
            //2,创建stateement对象
            stmt=conn.createStatement();
            //3，发送sql
            rs=stmt.executeQuery(sql);
            //4，处理结果
            List<Map<String,Object>> list=new ArrayList<>();
            while (rs.next()){
               // Map<String,Object> rowMap=new HashMap<>();
                //rowMap.put("id", rs.getInt("id"));
               // rowMap.put("name", rs.getString("name"));
               // rowMap.put("remark",rs.getString("remark") );
               // rowMap.put("createdTime",rs.getTimestamp("createdTime") );
                list.add(rowMap(rs));

            }
            return list;

        }catch (SQLException e){
            e.printStackTrace();
            throw new RuntimeException(e);//转换为非检查异常

        }finally {
            //5，释放资源
            //if(rs!=null)try {rs.close();}catch (Exception e){e.printStackTrace();}
           // if(stmt!=null)try {stmt.close();}catch (Exception e){e.printStackTrace();}
            //if(conn!=null)try {conn.close();}catch (Exception e){e.printStackTrace();}
            close(rs,stmt,conn);
        }


    }
    private Map<String,Object> rowMap(ResultSet rs)throws SQLException{
        Map<String,Object> rowMap=new HashMap<>();
        //方法1映射
        //rowMap.put("id",rs.getInt("id"));
        //rowMap.put("name",rs.getString("name"));
        //rowMap.put("remark",rs.getString("remark"));
        //rowMap.put("createdTime",rs.getTimestamp("createdTime"));
        //方法2映射
        ResultSetMetaData rsmd=rs.getMetaData();//获取元数据(包括表中的字段名)
        int columnCount=rsmd.getColumnCount();//获取列的数量
        for(int i=0;i<columnCount;i++){
            rowMap.put(rsmd.getColumnLabel(i+1),rs.getObject(rsmd.getColumnLabel(i+1)));
            //getColumnLabel(i+1);获取表中字段名或字段名对应的别名
        }
        return rowMap;
    }
    private void close(ResultSet rs,Statement stmt,Connection conn){
        if(rs!=null)try{rs.close();}catch(Exception e){e.printStackTrace();}
        if(stmt!=null)try{stmt.close();}catch(Exception e){e.printStackTrace();}
        //这里的连接是返回到了池中
        if(conn!=null)try{conn.close();}catch(Exception e){e.printStackTrace();}
    }


}
